package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.Calendar;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.FinanceSheet;
import Database.DatabaseConnector;

@WebServlet("/GetFinanceServlet")
public class GetFinancialServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			DatabaseConnector db = new DatabaseConnector();
			String business = (String) request.getSession().getAttribute("username");
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			System.out.println(business);
			Date low;
			Date high;
			
			try {
				Calendar calendar = Calendar.getInstance();
				if(start == null) {
					calendar.set(Calendar.MONTH, 0);
					calendar.set(Calendar.DAY_OF_MONTH, 1);
					low = new Date(calendar.getTimeInMillis());
				}
				else {
					String[] date = start.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
					low = new Date(calendar.getTimeInMillis());
				}
				
				calendar = Calendar.getInstance();
				if(end == null) {
					calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR)+1);
					calendar.set(Calendar.MONTH, 0);
					calendar.set(Calendar.DAY_OF_MONTH, 1);
					high = new Date(calendar.getTimeInMillis());
				}
				else {
					String[] date = end.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
					high = new Date(calendar.getTimeInMillis());
				}
			}
			catch (NumberFormatException e) {
				JSONObject result = new JSONObject();
				result.put("failed", true);
				result.put("message", "Incorrect Date Format");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			
			double amtOwe = db.getAmountOwing(null, business, low, high);
			double amtMade = db.getTotalMoneyMade(business, low, high);
			double wages = db.getTotalWages(business, low, high);
			
			FinanceSheet fSheet = new FinanceSheet(amtMade, wages, amtOwe, 0.13, low, high);
			
			JSONObject result = fSheet.getJSONObject();
			result.put("failed", false);
			
			response.getWriter().print(result);
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		}
		catch(NullPointerException e) {
			JSONObject result = new JSONObject();
			try {
				result.put("failed", true);
				result.put("message", "Null Pointer Exception");
				response.getWriter().print(result);
			} catch (JSONException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
		
		catch (JSONException e) {
			JSONObject result = new JSONObject();
			try {
				result.put("failed", true);
				result.put("message", "JSON Exception");
				response.getWriter().print(result);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
		catch(IOException e) {
			JSONObject result = new JSONObject();
			try {
				result.put("failed", true);
				result.put("message", "IO Exception");
				response.getWriter().print(result);
			} catch (JSONException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
		
	}
}
